'''
Date: 2021-10-12 17:50:49
__Author__: 离小镜
'''

import time
import random
import feapder
from datetime import datetime, timedelta
from feapder.db.mysqldb import MysqlDB

from items import compete_dy_good_note_item, compete_dy_good_author_item

class Monitorgoods(feapder.AirSpider):
    
    conner = MysqlDB(
    )
    def start_requests(self):
        
        sql = '''
              SELECT 
                promotion_id, title
              FROM
                compete_goods_account
              '''
        for good in self.conner.find(sql=sql):
            
            if good[1] is None:
                yield feapder.Request(method="GET", url=f"https://api-service.chanmama.com/v1/home/product/info?promotion_id={good[0]}", download_midware=self.header, promotion_id=good[0])
            payload = {
                "promotion_id": good[0],
                "orderby": "time",
                "keyword": "",
                "page": 1,
                "order": "desc",
                "filter_delete": 0,
                "page_size": 100,
                "start_time": str(datetime.now().date()-timedelta(days=6)),
                "end_time": str(datetime.now().date())
            }
            yield feapder.Request(method="POST", url="https://api-service.chanmama.com/v1/home/product/awemeList", json=payload, download_midware=self.header, callback=self.videoparse)
            time.sleep(random.uniform(1.1, 2.2))
            authorparam = {
                "product_id": good[0],
                "category": "",
                "reputation_level": "-1",
                "follower_count": "",
                "keyword": "",
                "page": "1",
                "size": "30",
                "start_date": str(datetime.now().date()-timedelta(days=6)),
                "end_date": str(datetime.now().date()),
            }
            yield feapder.Request(method="GET", url="https://api-service.chanmama.com/v1/product/author/analysis", params=authorparam, download_midware=self.header, callback=self.authorparase)
            #break
    
    def authorparase(self, request, response):
        
        if response.json.get('errCode') == 0:
            datajson = response.json.get('data')
            pagejson = datajson.get('page_info').get("totalPage")
            for author in datajson.get("list"):
                authoritem = compete_dy_good_author_item.CompeteDyGoodAuthorItem()
                authoritem.product_id = request.params.get('product_id')
                authoritem.uid = author.get('author_id')
                authoritem.avatar = author.get('avatar')
                authoritem.label = author.get('label')
                authoritem.nickname = author.get('nickname')
                authoritem.associate_videos = author.get('aweme_count')
                authoritem.forecast_amount = author.get('amount')
                authoritem.forecast_volume = author.get('volume')
                authoritem.fans = author.get('follower_count')
                authoritem.updated_at = datetime.now()
                yield authoritem
            
    
            if int(request.params.get('page')) < pagejson:
                localparams = request.params
                localparams["page"] = str(int(localparams["page"]) + 1)
                yield feapder.Request(method="GET", url="https://api-service.chanmama.com/v1/product/author/analysis", params=localparams, download_midware=self.header, callback=self.authorparase)
        
        

    def videoparse(self, request, response):
        
        if response.json.get('errCode') == 0:
            datajson = response.json.get('data')
            totalpage = datajson.get('page_info').get('totalPage')
            for awesome in datajson.get('search_results'):
                
                videos = awesome.get("aweme_info")
                author = awesome.get("author_info")
                product = awesome.get("product_info")
                videoitem = compete_dy_good_note_item.CompeteDyGoodNoteItem()
                
                
                videoitem.product_id = request.json.get("promotion_id")
                videoitem.content_id = videos.get("aweme_id")
                videoitem.content_title = videos.get("aweme_title")
                videoitem.content_url = videos.get("aweme_url")
                videoitem.uid = author.get("author_id")
                videoitem.nickname = author.get("nickname")
                videoitem.publish_time = videos.get("aweme_create_time")
                videoitem.amount = product.get("amount")
                videoitem.volume = product.get("volume")
                videoitem.support = videos.get("digg_count")
                videoitem.comment = videos.get("comment_count")
                videoitem.share   = videos.get("share_count")
                videoitem.updated_at = datetime.now()
                yield videoitem

                # authoritem = compete_dy_good_author_item.CompeteDyGoodAuthorItem()
                # authoritem.product_id = request.json.get("promotion_id")
                # authoritem.uid = author.get("author_id")
                # authoritem.nickname = author.get
                pass



    def header(self, request):

        request.headers = {
            'Cookie': self.conner.find(sql="""SELECT cookies FROM crawl_cookies WHERE name = 'cicada'""",limit=1)[0]
        }
    def parse(self, request, response):
        
        if response.json.get('errCode') == 0:
            goodjson = response.json.get('data').get('product')
            update = f'''
                        UPDATE 
                            compete_goods_account 
                        SET
                            title = "{goodjson.get('title')}", brand_name = "{goodjson.get('brand_name')}", shop_id = "{goodjson.get('shop_id')}", shop_name = "{goodjson.get('shop_name')}", updated_at = "{datetime.now()}"
                        WHERE
                            promotion_id = "{request.promotion_id}"
                     '''
            self.conner.update(sql=update)


if __name__ == "__main__":
    Monitorgoods().start()